<?xml version="1.0" encoding="Windows-1251"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="text" version="1.0" encoding="Windows-1251"/>

    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <root>
            <xsl:apply-templates/>
        </root>
    </xsl:template>

    <xsl:template match="domain/mailbox[position()>1]" mode="key">
        <xsl:value-of select="@name"/>
        <xsl:text>@</xsl:text>
    </xsl:template>

    <xsl:template match="domain/mailbox">
        <row>
            <key>
                <xsl:apply-templates select="." mode="key"/>
                <xsl:value-of select="ancestor::domain[1]/@name"/>
            </key>
            <value>
                <xsl:value-of select="@name"/>
            </value>
            <params>
                <xsl:apply-templates select="." mode="params"/>
            </params>
        </row>
    </xsl:template>

    <xsl:template match="node()" mode="params">
        <xsl:variable name="params" select="ancestor-or-self::node()/param"/>
        <xsl:for-each select="$params">
            <xsl:variable name="name" select="@name"/>
            <xsl:if test="not(preceding::node()/@name=$name)">
                <xsl:copy-of select="."/>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>